package com.ays.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ays.bean.*;
import com.ays.comm.Convert;
import com.ays.mapper.*;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Administrator
 * @date 2024/6/5
 * @desc 接收平台推送信息业务处理
 */
@Service
public class HisBusinessService {

    @Autowired
    private AtmDeptDictMapper atmDeptDictMapper;

    @Autowired
    private AtmHisUsersMapper atmHisUsersMapper;

    @Autowired
    private AtmOperationDictMapper atmOperationDictMapper;

    @Autowired
    private AtmOperationScheduleMapper atmOperationScheduleMapper;

    @Autowired
    private AtmPatMasterIndexMapper atmPatMasterIndexMapper;

    @Autowired
    private AtmPatsInHospitalMapper atmPatsInHospitalMapper;

    @Autowired
    private AtmScheduledOperationNameMapper atmScheduledOperationNameMapper;


    public String business(JSONObject body, String module){
        String msg = null;
        switch (module){
            case "AtmDeptDict":                             // 科室信息
                msg = atmDeptDict(body);
                break;
            case "AtmHisUsers":                             // 人员字典
                msg = atmHisUsers(body);
                break;
            case "AtmOperationDict":                        // 手术字典
                msg = atmOperationDict(body);
                break;
            case "AtmOperationSchedule":                    // 手术申请预约
                msg = atmOperationSchedule(body);
                break;
            case "AtmPatMasterIndex":                       // 病人基本信息
                msg = atmPatMasterIndex(body);
                break;
            case "AtmPatsInHospital":                       // 住院登记信息
                msg = atmPatsInHospital(body);
                break;
            default:
                break;
        }
        return msg;
    }

    public String atmDeptDict(JSONObject body){
        String msg = "ok";
        JSONObject CT_DeptList = body.get("CT_DeptList", JSONObject.class);
        if(CT_DeptList.get("CT_Dept") instanceof JSONArray){
            JSONArray array = CT_DeptList.get("CT_Dept", JSONArray.class);
            for(Object item : array){
                JSONObject json = (JSONObject) item;
                saveAtmDeptDict(json);
            }
        }else if(CT_DeptList.get("CT_Dept") instanceof JSONObject){
            JSONObject json = CT_DeptList.get("CT_Dept", JSONObject.class);
            saveAtmDeptDict(json);
        }
        return msg;
    }

    public void saveAtmDeptDict(JSONObject json){
        AtmDeptDict dict = Convert.covertAtmDeptDict(json);
        AtmDeptDict param = new AtmDeptDict();
        param.setDeptCode(dict.getDeptCode());
        List<AtmDeptDict> atmDeptDicts = atmDeptDictMapper.selectList(param);
        if(!ObjectUtils.isEmpty(atmDeptDicts)){
            atmDeptDictMapper.update(dict);
        }else {
            atmDeptDictMapper.insert(dict);
        }
    }

    public String atmHisUsers(JSONObject body){
        String msg = "ok";
        JSONObject CT_CareProvList = body.get("CT_CareProvList", JSONObject.class);
        if(CT_CareProvList.get("CT_CareProv") instanceof JSONArray){
            JSONArray array = CT_CareProvList.get("CT_CareProv", JSONArray.class);
            for(Object item : array){
                JSONObject json = (JSONObject) item;
                saveAtmHisUsers(json);
            }
        }else if(CT_CareProvList.get("CT_CareProv") instanceof JSONObject){
            JSONObject json = CT_CareProvList.get("CT_CareProv", JSONObject.class);
            saveAtmHisUsers(json);
        }
        return msg;
    }

    public void saveAtmHisUsers(JSONObject json){
        AtmHisUsers user = Convert.covertAtmHisUsers(json);
        AtmHisUsers param = new AtmHisUsers();
        param.setUserId(user.getUserId());
        List<AtmHisUsers> users = atmHisUsersMapper.selectList(param);
        if(!ObjectUtils.isEmpty(users)){
            atmHisUsersMapper.update(user);
        }else {
            atmHisUsersMapper.insert(user);
        }
    }

    public String atmOperationDict(JSONObject body){
        String msg = "ok";
        JSONObject CT_OperationList = body.get("CT_OperationList", JSONObject.class);
        if(CT_OperationList.get("CT_Operation") instanceof JSONArray){
            JSONArray array = CT_OperationList.get("CT_Operation", JSONArray.class);
            for(Object item : array){
                JSONObject json = (JSONObject) item;
                saveAtmOperationDict(json);
            }
        }else if(CT_OperationList.get("CT_Operation") instanceof JSONArray){
            JSONObject json = CT_OperationList.get("CT_Operation", JSONObject.class);
            saveAtmOperationDict(json);
        }
        return msg;
    }

    public void saveAtmOperationDict(JSONObject json){
        AtmOperationDict dict = Convert.covertAtmOperationDict(json);
        AtmOperationDict param = new AtmOperationDict();
        param.setOperationCode(dict.getOperationCode());
        List<AtmOperationDict> dicts = atmOperationDictMapper.selectList(param);
        if(!ObjectUtils.isEmpty(dicts)){
            atmOperationDictMapper.update(dict);
        }else {
            atmOperationDictMapper.insert(dict);
        }
    }

    public String atmOperationSchedule(JSONObject body){
        String msg = "ok";
        if(body.get("OperationArrangeRt") instanceof JSONObject) {
            JSONObject json = body.get("OperationArrangeRt", JSONObject.class);
            saveAtmOperationSchedule(json);

            JSONObject OPERATIONS = json.get("OPERATIONS", JSONObject.class);
            OPERATIONS.putOnce("PATPatientID",json.get("PATPatientID",String.class));
            OPERATIONS.putOnce("PAADMVisitNumber",json.get("PAADMVisitNumber",String.class));
            OPERATIONS.putOnce("SCHEDULE_ID",json.get("SCHEDULE_ID",String.class));
            saveAtmOperationScheduleName(OPERATIONS);
        } else if(body.get("OperationArrangeRt") instanceof JSONArray) {
            JSONArray array = body.get("OperationArrangeRt", JSONArray.class);
            for(Object item : array){
                JSONObject json = (JSONObject) item;
                saveAtmOperationSchedule(json);

                JSONObject OPERATIONS = json.get("OPERATIONS", JSONObject.class);
                OPERATIONS.putOnce("PATPatientID",json.get("PATPatientID",String.class));
                OPERATIONS.putOnce("PAADMVisitNumber",json.get("PAADMVisitNumber",String.class));
                OPERATIONS.putOnce("SCHEDULE_ID",json.get("SCHEDULE_ID",String.class));
                saveAtmOperationScheduleName(OPERATIONS);
            }
        }
        return msg;
    }

    public void saveAtmOperationSchedule(JSONObject json){
        AtmOperationSchedule schedule = Convert.covertAtmOperationSchedule(json);
        if("A".equals(schedule.getState()) || "C".equals(schedule.getState())){
            // N新建，C取消
            if("A".equals(schedule.getState())){
                schedule.setState("0");
            }else {
                schedule.setState("-1");
            }
            AtmOperationSchedule param = new AtmOperationSchedule();
            param.setScheduleId(schedule.getScheduleId());
            param.setPatientId(schedule.getPatientId());
            param.setVisitId(schedule.getVisitId());
            List<AtmOperationSchedule> schedules = atmOperationScheduleMapper.selectList(param);
            if(!ObjectUtils.isEmpty(schedules)){
                if("0".equals(schedules.get(0).getState())){
                    // 当为未提交的状态才允许更新
                    atmOperationScheduleMapper.delete(param);
                    atmOperationScheduleMapper.insert(schedule);
                }
            }else {
                atmOperationScheduleMapper.insert(schedule);
            }
        }

    }

    public String atmPatMasterIndex(JSONObject body){
        String msg = "ok";
        if( body.get("PatientRegistryRt") instanceof JSONArray){
            JSONArray array = body.get("PatientRegistryRt", JSONArray.class);
            for(Object item : array){
                JSONObject json = (JSONObject) item;
                saveAtmPatMasterIndex(json);
            }
        }else if( body.get("PatientRegistryRt") instanceof JSONObject){
            JSONObject json = body.get("PatientRegistryRt", JSONObject.class);
            saveAtmPatMasterIndex(json);
        }
        return msg;
    }

    public void saveAtmPatMasterIndex(JSONObject json){
        AtmPatMasterIndex master = Convert.covertAtmPatMasterIndex(json);
        AtmPatMasterIndex param = new AtmPatMasterIndex();
        param.setPatientId(master.getPatientId());
        List<AtmPatMasterIndex> masters = atmPatMasterIndexMapper.selectList(param);
        if(!ObjectUtils.isEmpty(masters)){
            atmPatMasterIndexMapper.update(master);
        }else {
            atmPatMasterIndexMapper.insert(master);
        }
    }

    public String atmPatsInHospital(JSONObject body){
        String msg = "ok";
        // 住院号
        String PATDocumentNo = body.get("PATPatientInfo", JSONObject.class).get("PATDocumentNo",String.class);
        // 保存住院信息
        if(body.get("InpatientEncounterStartedRt") instanceof JSONObject) {
            JSONObject json = body.get("InpatientEncounterStartedRt", JSONObject.class);
            json.putOnce("PATDocumentNo",PATDocumentNo);
            saveAtmPatsInHospital(json);
        }else if(body.get("InpatientEncounterStartedRt") instanceof JSONObject) {
            JSONArray array = body.get("InpatientEncounterStartedRt", JSONArray.class);
            for(Object item : array){
                JSONObject json = (JSONObject) item;
                json.putOnce("PATDocumentNo",PATDocumentNo);
                saveAtmPatsInHospital(json);
            }
        }
        // 保存住院信息附带的病人基本信息
        JSONObject entries = body.get("PATPatientInfo", JSONObject.class);
        AtmPatMasterIndex master = Convert.covertAtmPatMasterIndexWithInHospital(entries);
        AtmPatMasterIndex param = new AtmPatMasterIndex();
        param.setPatientId(master.getPatientId());
        List<AtmPatMasterIndex> masters = atmPatMasterIndexMapper.selectList(param);
        if(!ObjectUtils.isEmpty(masters)){
            atmPatMasterIndexMapper.update(master);
        }else {
            atmPatMasterIndexMapper.insert(master);
        }
        return msg;
    }

    public void saveAtmPatsInHospital(JSONObject json){
        AtmPatsInHospital inHospital = Convert.covertAtmPatsInHospital(json);
        AtmPatsInHospital param = new AtmPatsInHospital();
        param.setPatientId(inHospital.getPatientId());
        param.setVisitId(inHospital.getVisitId());
        List<AtmPatsInHospital> inHospitals = atmPatsInHospitalMapper.selectList(param);
        if(!ObjectUtils.isEmpty(inHospitals)){
            atmPatsInHospitalMapper.update(inHospital);
        }else {
            atmPatsInHospitalMapper.insert(inHospital);
        }
    }

    public void saveAtmOperationScheduleName(JSONObject json){
        AtmScheduledOperationName atmScheduledOperationName = Convert.covertAtmScheduledOperationName(json);
        AtmScheduledOperationName param = new AtmScheduledOperationName();
        param.setScheduleId(atmScheduledOperationName.getScheduleId());
        param.setPatientId(atmScheduledOperationName.getPatientId());
        param.setVisitId(atmScheduledOperationName.getVisitId());
        List<AtmScheduledOperationName> names = atmScheduledOperationNameMapper.selectList(param);
        if(!ObjectUtils.isEmpty(names)){
            atmScheduledOperationNameMapper.delete(param);
            atmScheduledOperationNameMapper.insert(atmScheduledOperationName);
        }else {
            atmScheduledOperationNameMapper.insert(atmScheduledOperationName);
        }
    }

}
